import { mockLLMModels } from '../data/llm'
import type { LLMModel, LLMKey, PageParams, PageResult } from '@/types'

export class MockLLMService {
  // 获取大模型列表
  static async getLLMModels(params: PageParams): Promise<PageResult<LLMModel>> {
    const { pageNum = 1, pageSize = 20, keyword = '' } = params
    
    let filteredModels = [...mockLLMModels]
    
    // 关键词搜索
    if (keyword) {
      filteredModels = filteredModels.filter(model => 
        model.displayName.toLowerCase().includes(keyword.toLowerCase()) ||
        model.providerName.toLowerCase().includes(keyword.toLowerCase())
      )
    }
    
    const total = filteredModels.length
    const start = (pageNum - 1) * pageSize
    const end = start + pageSize
    const records = filteredModels.slice(start, end)
    const pages = Math.ceil(total / pageSize)
    
    return {
      records,
      total,
      current: pageNum,
      size: pageSize,
      pages,
      hasPrevious: pageNum > 1,
      hasNext: pageNum < pages
    }
  }

  // 获取大模型详情
  static async getLLMModel(providerName: string): Promise<LLMModel> {
    const model = mockLLMModels.find(m => m.providerName === providerName)
    
    if (!model) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    return { ...model }
  }

  // 创建大模型
  static async createLLMModel(modelData: Partial<LLMModel>): Promise<LLMModel> {
    const newModel: LLMModel = {
      providerName: modelData.providerName || '',
      displayName: modelData.displayName || '',
      keys: [],
      keyCount: 0,
      activeKeyCount: 0,
      description: modelData.description || '',
      icon: modelData.icon || '',
      status: modelData.status || 'active'
    }
    
    mockLLMModels.push(newModel)
    return { ...newModel }
  }

  // 更新大模型
  static async updateLLMModel(providerName: string, modelData: Partial<LLMModel>): Promise<LLMModel> {
    const index = mockLLMModels.findIndex(m => m.providerName === providerName)
    
    if (index === -1) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    const updatedModel = {
      ...mockLLMModels[index],
      ...modelData
    }
    
    mockLLMModels[index] = updatedModel
    return { ...updatedModel }
  }

  // 删除大模型
  static async deleteLLMModel(providerName: string): Promise<void> {
    const index = mockLLMModels.findIndex(m => m.providerName === providerName)
    
    if (index === -1) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    mockLLMModels.splice(index, 1)
  }

  // 添加模型密钥
  static async addLLMKey(providerName: string, keyData: Partial<LLMKey>): Promise<LLMKey> {
    const model = mockLLMModels.find(m => m.providerName === providerName)
    
    if (!model) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    const newKey: LLMKey = {
      id: Date.now(),
      llmProvider: providerName,
      remark: keyData.remark || '',
      secretKey: keyData.secretKey || null,
      apiKey: keyData.apiKey || null,
      status: keyData.status || 'active',
      createTime: new Date().toISOString(),
      updateTime: null
    }
    
    model.keys.push(newKey)
    model.keyCount = model.keys.length
    model.activeKeyCount = model.keys.filter(k => k.status === 'active').length
    
    return { ...newKey }
  }

  // 删除模型密钥
  static async deleteLLMKey(providerName: string, keyId: number): Promise<void> {
    const model = mockLLMModels.find(m => m.providerName === providerName)
    
    if (!model) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    const keyIndex = model.keys.findIndex(k => k.id === keyId)
    
    if (keyIndex === -1) {
      throw new Error(`密钥 "${keyId}" 不存在`)
    }
    
    model.keys.splice(keyIndex, 1)
    model.keyCount = model.keys.length
    model.activeKeyCount = model.keys.filter(k => k.status === 'active').length
  }

  // 更新模型状态
  static async updateLLMModelStatus(providerName: string, status: 'active' | 'inactive'): Promise<LLMModel> {
    const model = mockLLMModels.find(m => m.providerName === providerName)
    
    if (!model) {
      throw new Error(`大模型 "${providerName}" 不存在`)
    }
    
    model.status = status
    
    return { ...model }
  }
}